﻿using System.Collections.Generic;
using System.Linq;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Repository.EntityRepository;

namespace KLS.PPDS.ApplicationService.Manager
{
   public class ContentImageManager:RepositoryManager<ContentImageRepository>
    {

       public static void Delete(ContentImage contentImage) 
       {
           Repository.Delete(contentImage);
           LogManager.LogDbDelete(contentImage);
       }

       public static void Save(ContentImage contentImage)
       {
           contentImage.InitAudit();

           if (contentImage.Id > 0) 
           {
               Repository.Update(contentImage);
               LogManager.LogDbUpdate(contentImage);
           }
           else if (contentImage.Id == 0) 
           {
               Repository.SaveAndEvict(contentImage);
               LogManager.LogDbAdd(contentImage);
           }

           
       }

       public static List<ContentImage> GetAllContentImage()
       {
           return Repository.GetAllContentImage();
       }

       public static ContentImage GetContentImageById(int contentImageId)
       {

           return Repository.GetContentImageById(contentImageId);
       }

       public static List<ContentImage> GetContentImageByContentId(int contentId)
       {
           return Repository.GetContentImageByContentId(contentId);
       }

       public static List<string> GetContentImageNamesByContentId(int contentId)
       {
           return Repository.GetContentImageByContentId(contentId).Select(c => c.DiskFileName).ToList();
       }

       public static ContentImage GetContentImageByIdAndDiskFileName(int ContentId, string DiskFileName)
       {
           return Repository.GetContentImageByIdAndDiskFileName(ContentId, DiskFileName);
           
       }

       public static bool Delete(int ContentId, string DiskFileName) 
       {
           var result = false;

           var x = GetContentImageByIdAndDiskFileName(ContentId, DiskFileName);

           if (x != null && x.Id > 0) 
           {
               LogManager.LogDbDelete(x);
               Repository.Delete(x);
               Repository.Flush();
               
           }

           
           return result;
       }
       
    }
}
